using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DPMP.ViewModel;
using System.Windows.Threading;
using EnvDTE;
using EnvDTE80;
using EnvDTE90;
using EnvDTE100;

namespace View
{
    /// <summary>
    /// Interaction logic for PatternExplorerControl.xaml
    /// </summary>
    public partial class PatternManagementExtensionControl : UserControl
    {
        EnvDTE80.DTE2 _dte;

        public PatternManagementExtensionControl(EnvDTE80.DTE2 dte)
            : this()
        {
            _dte = dte;
        }

        public PatternManagementExtensionControl()
        {
            InitializeComponent();

            // create the view model for the pattern model collection
            PatternModelCollectionViewModel patternModelCollection = new PatternModelCollectionViewModel();

            // bind the UI to the view model
            base.DataContext = patternModelCollection;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions")]
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(string.Format(System.Globalization.CultureInfo.CurrentUICulture, "We are inside {0}.button1_Click()", this.ToString()),
                            "PatternExplorer");

        }

        private static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
        {
            while (source != null && source.GetType() != typeof(T))
            {
                if (source is Visual || source is Visual3D)
                {
                    source = VisualTreeHelper.GetParent(source);
                }
                else
                {
                    source = LogicalTreeHelper.GetParent(source);
                }
            }
            return source;
        }

        /// <summary>
        /// Retrieve the parent of an item
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private static TreeViewItem GetParentTreeViewItem(DependencyObject item)
        {
            if (item != null)
            {
                DependencyObject parent = VisualTreeHelper.GetParent(item);
                TreeViewItem parentTreeViewItem = parent as TreeViewItem;
                return parentTreeViewItem ?? GetParentTreeViewItem(parent);
            }
            return null;
        }

        /// <summary>
        /// Invoked when a pattern model/instance is right-clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvDesignPattern_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem selectedItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
            TreeViewItem selectedItemParent = GetParentTreeViewItem(selectedItem as DependencyObject);
        }

        private void tvDesignPattern_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem item = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;

            if (item != null)
            {
                item.IsSelected = true;
                item.Focus();
                e.Handled = true;
            }
        }

        /// <summary>
        /// show corresponding properties when a pattern model/instance is selected
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvDesignPattern_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            // get the selected item
            TreeViewItem selectedItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;

            // check if selected item is null
            if (selectedItem == null)
            {
                // clear selection on the tree view if necessary
                if (tvDesignPattern.SelectedItem != null && tvDesignPattern.SelectedItem is TreeViewItemViewModel)
                {
                    (tvDesignPattern.SelectedItem as TreeViewItemViewModel).IsSelected = false;
                }

                // display no properties when no item is selected
                dgPatternProperties.DataContext = null;
            }
            else
            {
                // guarantee the selection of the item
                selectedItem.IsSelected = true;

                // set the data context to display corresponding properties (either pattern model/instance)
                dgPatternProperties.DataContext = (selectedItem.Header as TreeViewItemViewModel).GetProperties();
            }
        }

        private void OnBtnViewCodeClick(object sender, RoutedEventArgs e)
        {
            // get the current Visual Studio instance
            var itemOp = _dte.ItemOperations;

            // get the full path of the file name
            // var fullPath = Service.GetFullPath(fileName);
            var fullPath = @"C:\Users\Tao\AppData\Roaming\DPMP\Singleton\SingletonInstance1\SingletonInstance1.spc.xvcl";

            // open the file name
            itemOp.OpenFile(fullPath, EnvDTEConstants.vsViewKindTextView);
        }

        private void OnAddNewInstanceClick(object sender, RoutedEventArgs e)
        {
            var addNewInstanceWindow = new AddNewInstanceWindow(this);
            addNewInstanceWindow.ShowDialog();
            // System.Windows.MessageBox.Show("Testing Add New Instance");
        }
    }
}
